package com.bookmarket.controller;

import com.bookmarket.entity.Admin;
import com.bookmarket.entity.Module;
import com.bookmarket.service.LQModuleService;
import com.bookmarket.utils.FileUtils;
import com.bookmarket.utils.TemplateUtils;
import com.github.pagehelper.Page;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.*;

/**
 * 测试Freemarker
 */
@Controller
@RequestMapping("/test")
public class LQ_ModuleController {

    @Resource
    private LQModuleService moduleService;
    /**
     * 测试多文件上传
     */
    @RequestMapping("filesUpload")
    public String filesUpload(@RequestParam("files") MultipartFile[] files, Module module , HttpServletRequest request) {
        //添加新建的模型，返回ID
        module.setCreateTime(new Date());
        moduleService.addModule(module);

        //判断file数组不能为空并且长度大于0
        if(files!=null&&files.length>0){
            //循环获取file数组中得文件
            for(int i = 0;i<files.length;i++){
                MultipartFile file = files[i];
                System.out.println(file.getOriginalFilename());
                if(file.getOriginalFilename()==""||file.getOriginalFilename()==null){
                    System.out.println("尚未上传文件");
                }else{
                    moduleService.addPicture(module.getModuleId(),file.getOriginalFilename());
                    //保存文件
                    FileUtils.saveFile(file,request);
                }
            }
        }
        // 重定向
        return "redirect:/test/module.do";
    }

    /**
     * 查询所有模型，并返回视图
     * @return
     */
    @RequestMapping("/module")
    public ModelAndView returnModule(){
        ModelAndView mv = new ModelAndView();
        Page page = moduleService.queryModule();
        mv.setViewName("backstage/LQ_defend");
        mv.addObject("totalPage",page.getPages());
        mv.addObject("page",page);
        return mv;
    }

    /**
     * 分页查询所有模型
     * @return
     */
    @RequestMapping("/queryModule")
    public ModelAndView queryModuleByPage(Module module,Date start,Date end,int currentPage,int pageSize){
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        Page page = moduleService.queryModuleByPage(module,start,end,currentPage,pageSize);
        mv.addObject("page",page);
        return mv;
    }

    /**
     * 删除某模型
     * @param id
     * @return
     */
    @RequestMapping("/delModule")
    public ModelAndView delModule(int id){
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        moduleService.delModule(id);
        return mv;
    }

    /**
     * 获得单个模型
     */
    @RequestMapping("/queryOneModule")
    public ModelAndView queryOneModule(int id){
        ModelAndView mv = new ModelAndView();
        mv.addObject("module",moduleService.queryOneModule(id));
        mv.setViewName("backstage/LQ_modModule");
        return mv;
    }

    /**
     * 修改模型
     * @return
     */
    @RequestMapping("/modModule")
    public void modModule(Module module){
        moduleService.modModule(module);
    }

    /**
     * 使用模型
     * @param id
     * @return
     */
    @RequestMapping("/useModule")
    public ModelAndView useModule(int id){
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        moduleService.useModule(id);
        return mv;
    }

    /**
     * 禁用模型
     * @param id
     * @return
     */
    @RequestMapping("/badModule")
    public ModelAndView badModule(int id){
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        moduleService.badModule(id);
        return mv;
    }

    public LQModuleService getModuleService() {
        return moduleService;
    }

    public void setModuleService(LQModuleService moduleService) {
        this.moduleService = moduleService;
    }
}
